package org.dromara.project.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.service.AddressService;
import org.dromara.project.domain.ProAddress;
import org.springframework.stereotype.Service;
import org.dromara.project.mapper.ProAddressMapper;
import org.dromara.project.service.IProAddressService;
import org.dromara.project.domain.vo.ProAddressVo;
import java.util.List;
import org.springframework.util.StringUtils;
import cn.hutool.core.util.StrUtil;


/**
 * 地区选择Service业务层处理
 *
 * @author LUSH
 * @date 2025-05-27
 */
@RequiredArgsConstructor
@Service
public class ProAddressServiceImpl implements IProAddressService, AddressService {

    private final ProAddressMapper baseMapper;

    @Override
    public List<ProAddressVo> selectByParentId(Long parentId) {
        return baseMapper.selectVoList(
            new LambdaQueryWrapper<ProAddress>()
                .eq(ProAddress::getParentId, parentId)
        );
    }

    @Override
    public String regionIdToName(String regionId) {
        if (!StringUtils.hasText(regionId)) {
            return null;
        }
        String[] ids = regionId.split(",");
        StringBuilder result = new StringBuilder();
        for (String id : ids) {
            if (StrUtil.isNotBlank(id)) {
                ProAddress address = baseMapper.selectById(Long.valueOf(id));
                if (address != null && StrUtil.isNotBlank(address.getName())) {
                    if (!result.isEmpty()) {
                        result.append("/");
                    }
                    result.append(address.getName());
                }
            }
        }
        return result.toString();
    }
}
